package com.wenlin.myproduction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseActivity;
import com.wenlin.myproduction.base.BaseSwipeActivity;
import com.wenlin.myproduction.bean.LabelBean;
import com.wenlin.myproduction.bean.LabelsBean;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.ClickUtil;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.UI;
import com.wenlin.myproduction.util.ValCommon;
import com.wenlin.myproduction.widget.FaceTabButton;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 文琳
 * @Email 377979485@qq.com
 * @desc
 * @time 2023-12-26 周二 23:49
 */
public class LabelsViewActivity extends BaseSwipeActivity {
    @BindView(R.id.v_bg_good)
    View mVBgGood;
    @BindView(R.id.v_bg_bad)
    View mVBgBad;
    @BindView(R.id.ftb_good)
    FaceTabButton mFtbGood;
    @BindView(R.id.ftb_bad)
    FaceTabButton mFtbBad;
    @BindView(R.id.rl_container_good)
    RelativeLayout mRlContainerGood;
    @BindView(R.id.rl_container_bad)
    RelativeLayout mRlContainerBad;

    private static final int POSITION_GOOD = 0;
    private static final int POSITION_BAD = 1;
    private LinkedList<LabelBean> labelsSelected;
    private LabelsViewHolder mLabelsViewHolderGood, mLabelsViewHolderBad;
    private int currentPosition;//当前选中的VH的位置

    public static void start(BaseActivity activity) {
        Intent intent = new Intent(activity, LabelsViewActivity.class);
        if (activity != null && !activity.isDestroyed() && !activity.isFinishing())
            activity.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_labels_view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(ValCommon.LABELS_VIEW);
        setData();
    }

    private void setData() {
        mFtbGood.setStateSelected();//默认选中
        mFtbGood.setOnSelectedListener(new FaceTabButton.OnSelectedListener() {
            @Override
            public void onSelectedStart() {
                mFtbBad.setLocked(true);
                mFtbBad.resetAnimAndUnselected();
                if (currentPosition != POSITION_GOOD) {
                    UI.setVisible(mVBgGood);
                    UI.setInvisible(mVBgBad);
                }
                //如果切换后，另一个标签列表已有选中，则此标签列表重置
                if (currentPosition != POSITION_GOOD && getCurrentLabelsSelected() != null && getCurrentLabelsSelected().size() > 0) {
                    if (mLabelsViewHolderGood != null) mLabelsViewHolderGood.resetLabels();
                }
                UI.setVisible(mRlContainerGood);
                UI.setGone(mRlContainerBad);
                currentPosition = POSITION_GOOD;
            }

            @Override
            public void onSelectedEnd() {
                mFtbBad.setLocked(false);
            }
        });
        mFtbBad.setBad(true);
        mFtbBad.setOnSelectedListener(new FaceTabButton.OnSelectedListener() {
            @Override
            public void onSelectedStart() {
                mFtbGood.setLocked(true);
                mFtbGood.resetAnimAndUnselected();
                if (currentPosition != POSITION_BAD) {
                    UI.setVisible(mVBgBad);
                    UI.setInvisible(mVBgGood);
                }
                //如果切换后，另一个标签列表已有选中，则此标签列表重置
                if (currentPosition != POSITION_BAD && getCurrentLabelsSelected() != null && getCurrentLabelsSelected().size() > 0) {
                    if (mLabelsViewHolderBad != null) mLabelsViewHolderBad.resetLabels();
                }
                UI.setVisible(mRlContainerBad);
                UI.setGone(mRlContainerGood);
                currentPosition = POSITION_BAD;
            }

            @Override
            public void onSelectedEnd() {
                mFtbGood.setLocked(false);
            }
        });
        labelsSelected = new LinkedList<>();

        // fake data
        List<LabelBean> labelsGood = new ArrayList<>();
        labelsGood.add(new LabelBean(0, "积极乐观", "#00A0E9"));
        labelsGood.add(new LabelBean(1, "乐于助人", "#2fc989"));
        labelsGood.add(new LabelBean(2, "坚持不懈", "#ffaa1e"));
        labelsGood.add(new LabelBean(3, "刻苦努力", "#8463FF"));
        labelsGood.add(new LabelBean(4, "认真细心", "#FF646F"));
        labelsGood.add(new LabelBean(5, "重视承诺", "#9D4E2D"));
        labelsGood.add(new LabelBean(6, "言出必行", "#4c5fbf"));
        labelsGood.add(new LabelBean(7, "表里如一", "#ffaa1e"));

        List<LabelBean> labelsBad = new ArrayList<>();
        labelsBad.add(new LabelBean(10, "消极悲观", "#666666"));
        labelsBad.add(new LabelBean(11, "冷漠无情", "#666666"));
        labelsBad.add(new LabelBean(12, "轻易放弃", "#666666"));
        labelsBad.add(new LabelBean(13, "满嘴跑火车", "#666666"));
        labelsBad.add(new LabelBean(14, "不讲卫生", "#666666"));
        labelsBad.add(new LabelBean(15, "好动不安", "#666666"));

        LabelsBean labels = new LabelsBean();
        labels.setLimit(2);
        labels.setGood(labelsGood);
        labels.setBad(labelsBad);
        //fake data end

        if (mLabelsViewHolderGood == null) {
            mLabelsViewHolderGood = new LabelsViewHolder(this, mRlContainerGood, labels.getLimit(), labels.getGood());
            mLabelsViewHolderGood.addToParent();//默认显示好评标签列表
            UI.setVisible(mVBgGood);
            UI.setInvisible(mVBgBad);
            UI.setVisible(mRlContainerGood);
            UI.setGone(mRlContainerBad);
            currentPosition = POSITION_GOOD;
        }
        if (mLabelsViewHolderBad == null) {
            mLabelsViewHolderBad = new LabelsViewHolder(this, mRlContainerBad, labels.getLimit(), labels.getBad());
            mLabelsViewHolderBad.addToParent();
        }
    }

    private LinkedList<LabelBean> getCurrentLabelsSelected() {
        return currentPosition == POSITION_BAD ? (mLabelsViewHolderBad != null ? mLabelsViewHolderBad.getSelectedLabels() : null) : (mLabelsViewHolderGood != null ? mLabelsViewHolderGood.getSelectedLabels() : null);
    }

    @OnClick({R.id.btn_confirm})
    public void click(View view) {
        if (view.getId() == R.id.btn_confirm) {
            if (!ClickUtil.canClick()) return;
            labelsSelected = getCurrentLabelsSelected();
            StringBuilder sb = new StringBuilder();
            if (Checker.hasList(labelsSelected)) {
                for (LabelBean lb :
                        labelsSelected) {
                    sb.append(lb.getName()).append(",");
                }
                String labels = sb.toString();
                if (labels.endsWith(",")) labels = labels.substring(0, labels.length() - 1);
                ToastUtils.getInstance().showInfo("提交成功：" + labels);
            }
        }
    }
}
